package model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import model.bean.Chucvu;

public class ChucvuDAO extends BaseDAO {

	/**
	 * Hiển thị danh sách cơ quan
	 * 
	 * @return ArrayList<dsCoquan>
	 * 
	 *         TriLD
	 */
	public int getChucvuID() {
		openDatabase();
		qry = "SELECT idchucvu FROM mockdb.chucvu ORDER BY idchucvu DESC LIMIT 1";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();

			int id;
			while (rs.next()) {
				id = rs.getInt("idchucvu");
				int chucvuID = id + 1;
				return chucvuID;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeDatabase();
		}
		return 1;
	}

	public boolean themCV(int id, String tencv) {

		boolean success = false;
		openDatabase();
		qry = "INSERT INTO mockdb.chucvu (idchucvu,tenchucvu) VALUES('" + id
				+ "',N'" + tencv + "')";
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			success = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return success;
	}

	/**
	 * Hiển thị danh sách chức vụ
	 * 
	 * @return ArrayList<dsChucvu>
	 * 
	 *         TriLD
	 */
	public ArrayList<Chucvu> dsChucvu() {

		ArrayList<Chucvu> dsChucvu = new ArrayList<Chucvu>();
		openDatabase();
		qry = "SELECT * FROM mockdb.chucvu order by idchucvu asc";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();
			while (rs.next()) {

				Chucvu cv = new Chucvu();

				cv.setId(rs.getString("idchucvu"));
				cv.setTencv(rs.getString("tenchucvu"));

				dsChucvu.add(cv);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dsChucvu;
	}

	public Chucvu getChucvu(String id) {

		Chucvu chucvu = new Chucvu();
		openDatabase();
		qry = "select * from mockdb.chucvu where idchucvu = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();

			while (rs.next()) {
				chucvu.setId(id);
				chucvu.setTencv(rs.getString("tenchucvu"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return chucvu;
	}

	public boolean capnhatChucvu(String id, String tencv) {

		openDatabase();
		qry = "UPDATE mockdb.chucvu SET tenchucvu = '" + tencv
				+ "' WHERE idchucvu = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	public boolean xoaChucvu(String id) {

		openDatabase();
		qry = "DELETE FROM mockdb.chucvu WHERE idchucvu = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

}
